from typing import Tuple, Dict


async def init_browser_context():
    try:
        from playwright.async_api import async_playwright
    except Exception:
        return None
    pw = await async_playwright().start()
    browser = await pw.chromium.launch(headless=True)
    context = await browser.new_context()
    return context


async def get_cookies_for(context, url: str) -> Tuple[str, Dict[str, str]]:
    try:
        await context.add_cookies([])
        await context.new_page()
        cookies = await context.cookies()
        from app.services.fames_spider.vendor.mindspider.tools.utils import convert_cookies
        return convert_cookies(cookies)
    except Exception:
        return ('', {})
